
var content;
//------ -----------------------提交时校验-----------------------------
$("#submit").on("click",function () {
    var formData = new FormData();
    var reg = /^[0-9a-zA-Z]+$/;
    var acupoint_id=$("#acupoint_id").val();
    if ( $("#acupoint_explain").val().length>255 || $("#acupoint_effect").val().length>255 || $("#acupoint_name").val().length>255 || $("#acupoint_id").val()=="" || $("#acupoint_name").val()=="" || $("#acupoint_effect").val()=="" || !reg.test(acupoint_id)) {
        if (!reg.test(acupoint_id)) {
            var add_check_message=document.getElementById("add_check_message");
            add_check_message.innerHTML="编号格式不对";
            add_check_message.style.fontSize=1+"px";
            add_check_message.style.color="red";
            add_check_message.style.visibility="visible";
        } else {
            if ($("#acupoint_explain").val().length>255 || $("#acupoint_effect").val().length>255 || $("#acupoint_name").val().length>255){
                if ($("#acupoint_explain").val().length>255){
                    var add_check_message=document.getElementById("add_check_message");
                    add_check_message.innerHTML="穴位说明长度不能超过255";
                    add_check_message.style.fontSize=1+"px";
                    add_check_message.style.color="red";
                    add_check_message.style.visibility="visible";
                }
                if($("#acupoint_effect").val().length>255){
                    var add_check_message=document.getElementById("add_check_message");
                    add_check_message.innerHTML="穴位功效长度不能超过255";
                    add_check_message.style.fontSize=1+"px";
                    add_check_message.style.color="red";
                    add_check_message.style.visibility="visible";
                }
                if ($("#acupoint_name").val().length>255){
                    var add_check_message=document.getElementById("add_check_message");
                    add_check_message.innerHTML="穴位名称长度不能超过255";
                    add_check_message.style.fontSize=1+"px";
                    add_check_message.style.color="red";
                    add_check_message.style.visibility="visible";
                }
            }else {
                var add_check_message=document.getElementById("add_check_message");
                add_check_message.innerHTML="填写完整信息";
                add_check_message.style.fontSize=1+"px";
                add_check_message.style.color="red";
                add_check_message.style.visibility="visible";
            }if ($("#acupoint_effect").val()==""){
                var add_check_acupEffect=document.getElementById("add_check_acupEffect");
                add_check_acupEffect.innerHTML="功效不能为空";
                add_check_acupEffect.style.fontSize=1+"px";
                add_check_acupEffect.style.color="red";
                add_check_acupEffect.style.visibility="visible";
            }
        }
        return false;
    }else {
        if ( addcheck_name() && addcheck_no() ){
            addcheck_name();
            addcheck_no();
            formData.append("acupNo",$("#acupoint_id").val());
            formData.append("acupName",$("#acupoint_name").val());
            formData.append("acupEffect",$("#acupoint_effect").val());
            formData.append("acupInstruct",$("#acupoint_explain").val());
            var  url="/Acupoint/addAcupoint";
            $.ajax(
                {
                    url: url,
                    type: 'POST',
                    cache: false,
                    data: formData,
                    processData: false,
                    contentType: false
                }
            ).done(
                function (res) {
                    location.reload(true);
                    $("#acupoint_id").val("");
                    $("#acupoint_name").val("");
                    $("#acupoint_effect").val("");
                    $("#acupoint_explain").val("");
                }
            ).fail(function (res) {
                alert("你的数据异常,添加失败");
            });
        }else {
            return false;
        }
    }
});
//删除穴位-----------------------------------
function acup_remov(id) {
    if (confirm("您确定删除当前穴位的数据？")) {
        $.ajax({
            type: "GET",
            url: "/Acupoint/deletAcupuncturePoint?acupId=" + id,
            dataType: "json"
        }).done(
            function (res) {

            }).fail(
            function (res) {
                location.reload(true);
            });
    }
}
//删除结束

//模态框赋值和更新数据------------------
var update_name;
var id_update;
var boll=true;
var boll2=true;
var no1;
function addcheck_no() {
    var formData = new FormData();
    var name = $("#acupoint_id").val();
    formData.append("acupNo", name);
    if (name==""){
        var add_check_acupNo=document.getElementById("add_check_acupNo");
        add_check_acupNo.innerHTML="编号不能为空";
        add_check_acupNo.style.fontSize=1+"px";
        add_check_acupNo.style.color="red";
        add_check_acupNo.style.visibility="visible";
    } else {
        var reg = /^[0-9a-zA-Z]+$/
        var str = $("#acupoint_id").val();
        if(!reg.test(str)){
            var add_check_acupNo=document.getElementById("add_check_acupNo");
            add_check_acupNo.innerHTML="非正确格式";
            add_check_acupNo.style.fontSize=1+"px";
            add_check_acupNo.style.color="red";
            add_check_acupNo.style.visibility="visible";
        }else {
            $.ajax({
                type: "POST",
                cache: false,
                url: "/Acupoint/findNo",
                data: formData,
                processData: false,
                contentType: false
            }).done(
                function (res) {
                    if (res.emp=="true") {
                        var add_check_acupNo=document.getElementById("add_check_acupNo");
                        add_check_acupNo.setAttribute("style","display:none");
                        boll2=true;
                    }else {
                        var add_check_acupNo=document.getElementById("add_check_acupNo");
                        add_check_acupNo.innerHTML="编号已存在";
                        add_check_acupNo.style.fontSize=1+"px";
                        add_check_acupNo.style.color="red";
                        add_check_acupNo.style.visibility="visible";
                        boll2=false;
                    }
                }
            );
        }
    }
    return boll2;
}
//---------------------------------穴位名称校验-----------------------
//1添加时校验
function addcheck_name() {
    var formData = new FormData();
    var name = $("#acupoint_name").val();
    if (name==""){
        //
        var add_check_acupoint_name=document.getElementById("add_check_acupoint_name");
        add_check_acupoint_name.innerHTML="穴位不能为空";
        add_check_acupoint_name.style.fontSize=1+"px";
        add_check_acupoint_name.style.color="red";
        add_check_acupoint_name.style.visibility="visible";
        //
    } else {
        formData.append("acupName", name);
        $.ajax({
            type: "POST",
            cache: false,
            url: "/Acupoint/findName",
            data: formData,
            processData: false,
            contentType: false
        }).done(
            function (res) {
                if (res.emp=="true") {
                    var add_check_acupoint_name=document.getElementById("add_check_acupoint_name");
                    add_check_acupoint_name.setAttribute("style","display:none");
                    boll=true;
                }else {
                    //
                    var add_check_acupoint_name=document.getElementById("add_check_acupoint_name");
                    add_check_acupoint_name.innerHTML="穴位已存在";
                    add_check_acupoint_name.style.fontSize=1+"px";
                    add_check_acupoint_name.style.color="red";
                    add_check_acupoint_name.style.visibility="visible";
                    //
                    boll=false;
                }
            }
        );
    }
    return boll;
}
//2更新时校验
function update_check_name() {
    var formData = new FormData();
    var name = $("#acupoint_name2").val();
    formData.append("acupName", name);
    if ( update_name==name ){
        boll2=true;
        var updat_check_acupName=document.getElementById("updat_check_acupName");
        updat_check_acupName.setAttribute("style","display:none");
    }else if (name!="") {
        $.ajax({
            type: "POST",
            cache: false,
            url: "/Acupoint/findName",
            data: formData,
            processData: false,
            contentType: false
        }).done(
            function (res) {
                if (res.emp=="true") {
                    var updat_check_acupName=document.getElementById("updat_check_acupName");
                    updat_check_acupName.setAttribute("style","display:none");
                    boll2=true;
                }else {
                    var updat_check_acupName=document.getElementById("updat_check_acupName");
                    updat_check_acupName.innerHTML="穴位已存在";
                    updat_check_acupName.style.fontSize=1+"px";
                    updat_check_acupName.style.color="red";
                    updat_check_acupName.style.visibility="visible";
                    boll2=false;
                }
            }
        );
    }else {
        var updat_check_acupName=document.getElementById("updat_check_acupName");
        updat_check_acupName.innerHTML="不能为空";
        updat_check_acupName.style.fontSize=1+"px";
        updat_check_acupName.style.color="red";
        updat_check_acupName.style.visibility="visible";
    }
    return boll2;
}
//--------------------------------点击编辑按钮----------------------------
function updateAcup(id) {
    $.ajax({
        type: "GET",
        url: "/Acupoint/findOnl?acupId="+id,
        dataType: "json",
        success: function (data) {
            id_update=data.emp.acupId;
            update_name=data.emp.acupName;
            $("#acupoint_id2").val(data.emp.acupNo);
            $("#acupoint_name2").val(data.emp.acupName);
            $("#acupoint_effect2").val(data.emp.acupEffect);
            $("#acupoint_explain2").val(data.emp.acupInstruct);
            no1=$("#acupoint_id2").val();
        },error: function () {
            alert("发送请求失败！");
        }
    });
};
//-----------------------------------------提交更新开始---------------------------
$("#submit2").on("click",function () {
    if (confirm("您确定更新当前数据？")){
        var bbb=true;
        var formData = new FormData();
        var reg = /^[0-9a-zA-Z]+$/;
        var acupoint_id=$("#acupoint_id2").val();
        if ( $("#acupoint_explain2").val().length>255 || $("#acupoint_effect2").val().length>255 ||
            $("#acupoint_name2").val().length>255 || $("#acupoint_id2").val()=="" ||
            $("#acupoint_name2").val()=="" || $("#acupoint_effect2").val()=="" || !reg.test(acupoint_id)) {
            if (!reg.test(acupoint_id)) {
                alert("编号的格式只能是英文或者数字")
            } else {
                if ($("#acupoint_explain2").val().length>255 || $("#acupoint_effect2").val().length>255 ||
                    $("#acupoint_name2").val().length>255){
                    if ($("#acupoint_explain2").val().length>255){
                        var updat_check_massage=document.getElementById("updat_check_massage");
                        updat_check_massage.innerHTML="穴位说明长度不能超过255";
                        updat_check_massage.style.fontSize=1+"px";
                        updat_check_massage.style.color="red";
                        updat_check_massage.style.visibility="visible";
                    }
                    if($("#acupoint_effect2").val().length>255){
                        var updat_check_massage=document.getElementById("updat_check_massage");
                        updat_check_massage.innerHTML="穴位功效长度不能超过255";
                        updat_check_massage.style.fontSize=1+"px";
                        updat_check_massage.style.color="red";
                        updat_check_massage.style.visibility="visible";
                    }
                    if ($("#acupoint_name2").val().length>255){
                        var updat_check_massage=document.getElementById("updat_check_massage");
                        updat_check_massage.innerHTML="穴位名称长度不能超过255";
                        updat_check_massage.style.fontSize=1+"px";
                        updat_check_massage.style.color="red";
                        updat_check_massage.style.visibility="visible";
                    }
                }else {
                    var updat_check_massage=document.getElementById("updat_check_massage");
                    updat_check_massage.innerHTML="填写完整信息";
                    updat_check_massage.style.fontSize=1+"px";
                    updat_check_massage.style.color="red";
                    updat_check_massage.style.visibility="visible";
                }
            }
            return false;
        }else {
            formData.append("acupId",id_update);
            formData.append("acupNo", $("#acupoint_id2").val());
            formData.append("acupName", $("#acupoint_name2").val().replace(" ",""));
            formData.append("acupEffect", $("#acupoint_effect2").val());
            formData.append("acupInstruct", $("#acupoint_explain2").val());
            var url = "/Acupoint/updateAcupuncture";
            //---------------------直接提交时的判断------------------
            var formData2 = new FormData();
            var name=$("#acupoint_id2").val();
            formData2.append("acupNo", name);
            if (update_check_name()){
                //-------------相同时直接上传---
                $.ajax(
                    {
                        url: url,
                        type: 'POST',
                        cache: false,
                        data: formData,
                        processData: false,
                        contentType: false
                    }
                ).done(
                    function (res) {
                        location.reload(true);
                    }
                ).fail(function (res) {
                    alert("你的数据异常,更新错误");
                });
            }else {
                var updat_check_massage=document.getElementById("updat_check_massage");
                updat_check_massage.innerHTML="穴位已经存在";
                updat_check_massage.style.fontSize=1+"px";
                updat_check_massage.style.color="red";
                updat_check_massage.style.visibility="visible";
            }
        }
        return false;
    }else {
        return false;
    }
});
//添加时功效校验
function add_acupEffect() {
    if ($("#acupoint_effect").val()==""){
        var add_check_acupEffect=document.getElementById("add_check_acupEffect");
        add_check_acupEffect.innerHTML="功效不能为空";
        add_check_acupEffect.style.fontSize=1+"px";
        add_check_acupEffect.style.color="red";
        add_check_acupEffect.style.visibility="visible";
    }else {
        var add_check_acupEffect=document.getElementById("add_check_acupEffect");
        add_check_acupEffect.setAttribute("style","display:none");
    }
}
//更新时功效校验
function updat_acupEffect() {
    if ($("#acupoint_effect2").val()==""){
        var updat_check_acupEffect=document.getElementById("updat_check_acupEffect");
        updat_check_acupEffect.innerHTML="功效不能为空";
        updat_check_acupEffect.style.fontSize=1+"px";
        updat_check_acupEffect.style.color="red";
        updat_check_acupEffect.style.visibility="visible";
    }else {
        var updat_check_acupEffect=document.getElementById("updat_check_acupEffect");
        updat_check_acupEffect.setAttribute("style","display:none");
    }
}
//添加时编号校验-------------------------------
function no() {
    var  addcheck=$("#acupoint_id2").val();
    if ( no1==addcheck ){
        return boll;
    }else {
        var formData = new FormData();
        var name=$("#acupoint_id2").val();
        formData.append("acupNo", name);
        $.ajax({
            type: "POST",
            cache: false,
            url: "/Acupoint/findNo",
            data: formData,
            processData: false,
            contentType: false
        }).done(
            function (res) {
                if (res.emp=="true") {
                    boll=true;
                }else {
                    var add_check_acupNo=document.getElementById("add_check_acupNo");
                    add_check_acupNo.innerHTML="编码已存在";
                    add_check_acupNo.style.fontSize=1+"px";
                    add_check_acupNo.style.color="red";
                    add_check_acupNo.style.visibility="visible";
                    boll=false;
                }
            }
        );
        return boll;
    }
}
//穴位功效校验

//校验结束------------------------------------------
//---------------------------------每一个框的单独校验------------------------------------
//1提交时的校验
function start_id(acupId,state){
    var formData = new FormData();
    formData.append("acupId", acupId);
    formData.append("state",state);
    if (state==1){
        $.ajax({
            type: "POST",
            cache: false,
            url: "/Acupoint/startAcup",
            data: formData,
            processData: false,
            contentType: false
        }).done(
            function (res) {
                location.reload(true);
            }
        ).error(function () {
            alert("禁用失败");
        });
    } else {
        $.ajax({
            type: "POST",
            cache: false,
            url: "/Acupoint/startAcup",
            data: formData,
            processData: false,
            contentType: false
        }).done(
            function (res) {
                location.reload(true);
            }
        ).error(function () {
            alert("启用失败");
        });
    }
}

//模糊查询-------------------
$("#searchBtn").on("click",function () {
    search();
});
//回车事件--------------------------------------------
$("#searchContent").bind('keypress',function (event) {
    if (event.keyCode=="13"){
        search();
    }
});
//搜搜------------------------------------
function search() {
    var type = $("#searchType").val();
    content = $("#searchContent").val();
    $.cookie("content",content,1);
    if (content === null || content === undefined || content === '' || content.length === 0){
        alert(type+"和"+content);
    }else {
        if (type=="acupNo") {
            var reg = /^[0-9a-zA-Z]+$/;
            if (!reg.test(content)){
                alert("编号只能是数字或者字母");
                $("#searchContent").val("");
            }else {
                window.location.href="/Acupoint/searchAcup?type="+type+"&content="+content;
                $.ajax({
                    type: 'POST',
                    data: {"type": type, "content": content},
                    url: "/Acupoint/searchAcup",
                    cache: false,
                    success: function (data) {
                        $("table").remove();
                        $(".content_pageinfor_ul2").remove();
                        $(".content_pageinfor_ul").remove();
                        var $data = $(data); // 将整个文档转化为jquery对象
                        var content = $data.find("table");
                        var content_pageinfor=$data.find(".content_pageinfor_ul2");
                        $(".content_pageinfor2").append(content_pageinfor);
                        $(".tableDiv").append(content);
                        $("#searchContent").val(content);
                    },
                    error: function () {
                        alert("请求失败！");
                    }
                });
            }
        }else {
            window.location.href="/Acupoint/searchAcup?type="+type+"&content="+content;
            $.ajax({
                type: 'POST',
                data: {"type": type, "content": content},
                url: "/Acupoint/searchAcup",
                cache: false,
                success: function (data) {
                    $("table").remove();
                    $(".content_pageinfor_ul2").remove();
                    $(".content_pageinfor_ul").remove();
                    var $data = $(data); // 将整个文档转化为jquery对象
                    var content = $data.find("table");
                    var content_pageinfor=$data.find(".content_pageinfor_ul2");
                    $(".content_pageinfor2").append(content_pageinfor);
                    $(".tableDiv").append(content);
                    $("#searchContent").val(content);
                },
                error: function () {
                    alert("请求失败！");
                }
            });
        }
    }
}
